/***************************************************************************************
 The copyright in this software is being made available under the BSD License, included
 below. This software may be subject to other third party rights,including patent rights,
 and no such rights are granted under this license.

 Regents of the Tsinghua University, Peking University, Zhejiang University, HKUST
 Tsinghua University, Peking University, Zhejiang University, HKUST 2011

 Copyright (c) 2011, Regents of the Tsinghua University, Peking University, Zhejiang
 University, HKUST. All Rights Reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:
   * Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution.
   * Neither the names of the copyright holders nor the names of its
     contributors may be used to endorse or promote products derived from this
     software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 OF SUCH DAMAGE.
***************************************************************************************/


#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <sys/timeb.h>
#include <string.h>
#include <memory.h>
#include <assert.h>
#if defined(WIN32) && !defined(__MINGW32__)
#include <IO.h>
#endif

#include "contributors.h"
#include "global.h"
#include "memalloc.h"
#include "ratectl.h"
#include "vlc.h"
#include "header.h"
#include "macroblock.h"

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
static void padding_img( byte **org_img )
{
    int x, y;
    int x_offset, y_offset;
    x_offset = IMG_PAD_SIZE + img->width;
    y_offset = IMG_PAD_SIZE + img->height-1;
    for ( x = 0; x < IMG_PAD_SIZE; x++ )
    {
        //the first row
        org_img[IMG_PAD_SIZE][x]          = org_img[IMG_PAD_SIZE][IMG_PAD_SIZE];
        org_img[IMG_PAD_SIZE][x+x_offset] = org_img[IMG_PAD_SIZE][x+x_offset-1];
        //the last row
        org_img[y_offset][x]              = org_img[y_offset][IMG_PAD_SIZE];
        org_img[y_offset][x+x_offset]     = org_img[y_offset][x+x_offset-1];
    }
    //vertical padding
    for ( y = IMG_PAD_SIZE-1; y >= 0; y-- )
    {
        memcpy( org_img[y], org_img[IMG_PAD_SIZE], img->p_width*sizeof( byte ) );
        memcpy( org_img[y_offset+y+1], org_img[y_offset], img->p_width*sizeof( byte ) );
    }
    for ( y=IMG_PAD_SIZE+1; y<img->height+IMG_PAD_SIZE; y++ )
    {
        for ( x=0; x<IMG_PAD_SIZE; x++ )
        {
            org_img[y][x]          = org_img[y][IMG_PAD_SIZE];
            org_img[y][x+x_offset] = org_img[y][x+x_offset-1];
        }
    }
}
#if QUARTER_wangIF
static void up_sampling()
{
	int x, y, pos;
	byte ***rec = img_y_rec_space;

	padding_img( img_y_rec_space[MAX_REF_NUM] );

	//interpolation

	for ( y = 1; y < img->p_height-2; y++ )//8tap
	{
		for ( x = 1; x < img->p_width-2; x++ )
		{
			//x1=RFM_CLIP3(x,minx,maxx);
			//y1=RFM_CLIP3(y,miny,maxy);
			y_up_sampling[MAX_REF_NUM][P_A][y][x] = INTERPOLATE_8tap_HALF( rec[MAX_REF_NUM][y][RFM_CLIP3( x-3, 1, img->p_width - 1 )], rec[MAX_REF_NUM][y][RFM_CLIP3( x-2, 1, img->p_width - 1 )], rec[MAX_REF_NUM][y][x-1], rec[MAX_REF_NUM][y][x], rec[MAX_REF_NUM][y][x+1], rec[MAX_REF_NUM][y][x+2], rec[MAX_REF_NUM][y][RFM_CLIP3( x+3, 1, img->p_width - 1 )],rec[MAX_REF_NUM][y][RFM_CLIP3( x+4, 1, img->p_width - 1 )] );
			y_up_sampling[MAX_REF_NUM][P_B][y][x] = INTERPOLATE_8tap_HALF( rec[MAX_REF_NUM][RFM_CLIP3( y-3, 1, img->p_height - 1 )][x],rec[MAX_REF_NUM][RFM_CLIP3( y-2, 1, img->p_height - 1 )][x],rec[MAX_REF_NUM][y-1][x], rec[MAX_REF_NUM][y][x], rec[MAX_REF_NUM][y+1][x], rec[MAX_REF_NUM][y+2][x] , rec[MAX_REF_NUM][RFM_CLIP3( y+3, 1, img->p_height - 1 )][x], rec[MAX_REF_NUM][RFM_CLIP3( y+4, 1, img->p_height - 1 )][x] );
#if QUARTER_PIXEL
			y_up_sampling[MAX_REF_NUM][P_A1][y][x] = INTERPOLATE_8tap_QUATER1( rec[MAX_REF_NUM][y][RFM_CLIP3( x-3, 1, img->p_width - 1 )], rec[MAX_REF_NUM][y][RFM_CLIP3( x-2, 1, img->p_width - 1 )], rec[MAX_REF_NUM][y][x-1], rec[MAX_REF_NUM][y][x], rec[MAX_REF_NUM][y][x+1], rec[MAX_REF_NUM][y][x+2], rec[MAX_REF_NUM][y][RFM_CLIP3( x+3, 1, img->p_width - 1 )],rec[MAX_REF_NUM][y][RFM_CLIP3( x+4, 1, img->p_width - 1 )] );
			y_up_sampling[MAX_REF_NUM][P_A2][y][x] = INTERPOLATE_8tap_QUATER3( rec[MAX_REF_NUM][y][RFM_CLIP3( x-3, 1, img->p_width - 1 )], rec[MAX_REF_NUM][y][RFM_CLIP3( x-2, 1, img->p_width - 1 )], rec[MAX_REF_NUM][y][x-1], rec[MAX_REF_NUM][y][x], rec[MAX_REF_NUM][y][x+1], rec[MAX_REF_NUM][y][x+2], rec[MAX_REF_NUM][y][RFM_CLIP3( x+3, 1, img->p_width - 1 )],rec[MAX_REF_NUM][y][RFM_CLIP3( x+4, 1, img->p_width - 1 )] );
			y_up_sampling[MAX_REF_NUM][P_B1][y][x] = INTERPOLATE_8tap_QUATER1( rec[MAX_REF_NUM][RFM_CLIP3( y-3, 1, img->p_height - 1 )][x],rec[MAX_REF_NUM][RFM_CLIP3( y-2, 1, img->p_height - 1 )][x],rec[MAX_REF_NUM][y-1][x], rec[MAX_REF_NUM][y][x], rec[MAX_REF_NUM][y+1][x], rec[MAX_REF_NUM][y+2][x] , rec[MAX_REF_NUM][RFM_CLIP3( y+3, 1, img->p_height - 1 )][x], rec[MAX_REF_NUM][RFM_CLIP3( y+4, 1, img->p_height - 1 )][x] );
			y_up_sampling[MAX_REF_NUM][P_B2][y][x] = INTERPOLATE_8tap_QUATER3( rec[MAX_REF_NUM][RFM_CLIP3( y-3, 1, img->p_height - 1 )][x],rec[MAX_REF_NUM][RFM_CLIP3( y-2, 1, img->p_height - 1 )][x],rec[MAX_REF_NUM][y-1][x], rec[MAX_REF_NUM][y][x], rec[MAX_REF_NUM][y+1][x], rec[MAX_REF_NUM][y+2][x] , rec[MAX_REF_NUM][RFM_CLIP3( y+3, 1, img->p_height - 1 )][x], rec[MAX_REF_NUM][RFM_CLIP3( y+4, 1, img->p_height - 1 )][x] );
#endif
		}
	}

	y=0;
	for( x=1; x<img->p_width-2; x++ )
	{
		y_up_sampling[MAX_REF_NUM][P_A][0][x]=y_up_sampling[MAX_REF_NUM][P_A][1][x];
		y_up_sampling[MAX_REF_NUM][P_B][0][x]=y_up_sampling[MAX_REF_NUM][P_B][1][x];
		y_up_sampling[MAX_REF_NUM][P_A][img->p_height-2][x]=y_up_sampling[MAX_REF_NUM][P_A][img->p_height-3][x];
		y_up_sampling[MAX_REF_NUM][P_B][img->p_height-2][x]=y_up_sampling[MAX_REF_NUM][P_B][img->p_height-3][x];
		y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x]=y_up_sampling[MAX_REF_NUM][P_A][img->p_height-3][x];
		y_up_sampling[MAX_REF_NUM][P_B][img->p_height-1][x]=y_up_sampling[MAX_REF_NUM][P_B][img->p_height-3][x];
#if QUARTER_PIXEL
		y_up_sampling[MAX_REF_NUM][P_A1][0][x]=y_up_sampling[MAX_REF_NUM][P_A1][1][x];
		y_up_sampling[MAX_REF_NUM][P_B1][0][x]=y_up_sampling[MAX_REF_NUM][P_B1][1][x];
		y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-2][x]=y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-3][x];
		y_up_sampling[MAX_REF_NUM][P_B1][img->p_height-2][x]=y_up_sampling[MAX_REF_NUM][P_B1][img->p_height-3][x];
		y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x]=y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-3][x];
		y_up_sampling[MAX_REF_NUM][P_B1][img->p_height-1][x]=y_up_sampling[MAX_REF_NUM][P_B1][img->p_height-3][x];

		y_up_sampling[MAX_REF_NUM][P_A2][0][x]=y_up_sampling[MAX_REF_NUM][P_A2][1][x];
		y_up_sampling[MAX_REF_NUM][P_B2][0][x]=y_up_sampling[MAX_REF_NUM][P_B2][1][x];
		y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-2][x]=y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-3][x];
		y_up_sampling[MAX_REF_NUM][P_B2][img->p_height-2][x]=y_up_sampling[MAX_REF_NUM][P_B2][img->p_height-3][x];
		y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x]=y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-3][x];
		y_up_sampling[MAX_REF_NUM][P_B2][img->p_height-1][x]=y_up_sampling[MAX_REF_NUM][P_B2][img->p_height-3][x];
#endif
	}
	for( y=0; y<img->p_height; y++ )
	{
		y_up_sampling[MAX_REF_NUM][P_A][y][0]=y_up_sampling[MAX_REF_NUM][P_A][y][1];
		y_up_sampling[MAX_REF_NUM][P_B][y][0]=y_up_sampling[MAX_REF_NUM][P_B][y][1];
		y_up_sampling[MAX_REF_NUM][P_A][y][img->p_width-2]=y_up_sampling[MAX_REF_NUM][P_A][y][img->p_width-3];
		y_up_sampling[MAX_REF_NUM][P_B][y][img->p_width-2]=y_up_sampling[MAX_REF_NUM][P_B][y][img->p_width-3];
		y_up_sampling[MAX_REF_NUM][P_A][y][img->p_width-1]=y_up_sampling[MAX_REF_NUM][P_A][y][img->p_width-3];
		y_up_sampling[MAX_REF_NUM][P_B][y][img->p_width-1]=y_up_sampling[MAX_REF_NUM][P_B][y][img->p_width-3];
#if QUARTER_PIXEL
		y_up_sampling[MAX_REF_NUM][P_A1][y][0]=y_up_sampling[MAX_REF_NUM][P_A1][y][1];
		y_up_sampling[MAX_REF_NUM][P_B1][y][0]=y_up_sampling[MAX_REF_NUM][P_B1][y][1];
		y_up_sampling[MAX_REF_NUM][P_A1][y][img->p_width-2]=y_up_sampling[MAX_REF_NUM][P_A1][y][img->p_width-3];
		y_up_sampling[MAX_REF_NUM][P_B1][y][img->p_width-2]=y_up_sampling[MAX_REF_NUM][P_B1][y][img->p_width-3];
		y_up_sampling[MAX_REF_NUM][P_A1][y][img->p_width-1]=y_up_sampling[MAX_REF_NUM][P_A1][y][img->p_width-3];
		y_up_sampling[MAX_REF_NUM][P_B1][y][img->p_width-1]=y_up_sampling[MAX_REF_NUM][P_B1][y][img->p_width-3];

		y_up_sampling[MAX_REF_NUM][P_A2][y][0]=y_up_sampling[MAX_REF_NUM][P_A2][y][1];
		y_up_sampling[MAX_REF_NUM][P_B2][y][0]=y_up_sampling[MAX_REF_NUM][P_B2][y][1];
		y_up_sampling[MAX_REF_NUM][P_A2][y][img->p_width-2]=y_up_sampling[MAX_REF_NUM][P_A2][y][img->p_width-3];
		y_up_sampling[MAX_REF_NUM][P_B2][y][img->p_width-2]=y_up_sampling[MAX_REF_NUM][P_B2][y][img->p_width-3];
		y_up_sampling[MAX_REF_NUM][P_A2][y][img->p_width-1]=y_up_sampling[MAX_REF_NUM][P_A2][y][img->p_width-3];
		y_up_sampling[MAX_REF_NUM][P_B2][y][img->p_width-1]=y_up_sampling[MAX_REF_NUM][P_B2][y][img->p_width-3];
#endif
	}


	for ( pos = P_A; pos <= P_B; pos++ )
	{
		//padding_img(y_up_sampling[MAX_REF_NUM][pos]);
	}

	for ( y = 1; y < img->p_height-2; y++ )
	{
		for ( x = 0; x < img->p_width; x++ )
		{
			//x1=RFM_CLIP3(x,minx-1,maxx+1);
			//y1=RFM_CLIP3(y,miny-1,maxy+1);
			//C
			y_up_sampling[MAX_REF_NUM][P_C][y][x] =  INTERPOLATE_6tap_HALF(    y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y-3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y-2, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A][y-1][x], y_up_sampling[MAX_REF_NUM][P_A][y][x], y_up_sampling[MAX_REF_NUM][P_A][y+1][x], y_up_sampling[MAX_REF_NUM][P_A][y+2][x], y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y+3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y+4, 1, img->p_height - 1 )][x] );

#if QUARTER_PIXEL
			//C1,D1,C3
			y_up_sampling[MAX_REF_NUM][P_C1][y][x] = INTERPOLATE_6tap_QUATER1( y_up_sampling[MAX_REF_NUM][P_A1][RFM_CLIP3( y-3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A1][RFM_CLIP3( y-2, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A1][y-1][x], y_up_sampling[MAX_REF_NUM][P_A1][y][x], y_up_sampling[MAX_REF_NUM][P_A1][y+1][x], y_up_sampling[MAX_REF_NUM][P_A1][y+2][x], y_up_sampling[MAX_REF_NUM][P_A1][RFM_CLIP3( y+3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A1][RFM_CLIP3( y+4, 1, img->p_height - 1 )][x] );
			y_up_sampling[MAX_REF_NUM][P_D1][y][x] = INTERPOLATE_6tap_HALF(    y_up_sampling[MAX_REF_NUM][P_A1][RFM_CLIP3( y-3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A1][RFM_CLIP3( y-2, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A1][y-1][x], y_up_sampling[MAX_REF_NUM][P_A1][y][x], y_up_sampling[MAX_REF_NUM][P_A1][y+1][x], y_up_sampling[MAX_REF_NUM][P_A1][y+2][x], y_up_sampling[MAX_REF_NUM][P_A1][RFM_CLIP3( y+3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A1][RFM_CLIP3( y+4, 1, img->p_height - 1 )][x] );
			y_up_sampling[MAX_REF_NUM][P_C3][y][x] = INTERPOLATE_6tap_QUATER3( y_up_sampling[MAX_REF_NUM][P_A1][RFM_CLIP3( y-3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A1][RFM_CLIP3( y-2, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A1][y-1][x], y_up_sampling[MAX_REF_NUM][P_A1][y][x], y_up_sampling[MAX_REF_NUM][P_A1][y+1][x], y_up_sampling[MAX_REF_NUM][P_A1][y+2][x], y_up_sampling[MAX_REF_NUM][P_A1][RFM_CLIP3( y+3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A1][RFM_CLIP3( y+4, 1, img->p_height - 1 )][x] );
			//

			//D3,,D4
			y_up_sampling[MAX_REF_NUM][P_D3][y][x] = INTERPOLATE_6tap_QUATER1( y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y-3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y-2, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A][y-1][x], y_up_sampling[MAX_REF_NUM][P_A][y][x], y_up_sampling[MAX_REF_NUM][P_A][y+1][x], y_up_sampling[MAX_REF_NUM][P_A][y+2][x], y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y+3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y+4, 1, img->p_height - 1 )][x] );
			y_up_sampling[MAX_REF_NUM][P_D4][y][x] = INTERPOLATE_6tap_QUATER3( y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y-3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y-2, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A][y-1][x], y_up_sampling[MAX_REF_NUM][P_A][y][x], y_up_sampling[MAX_REF_NUM][P_A][y+1][x], y_up_sampling[MAX_REF_NUM][P_A][y+2][x], y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y+3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y+4, 1, img->p_height - 1 )][x] );

			//C2,D2,C4
			y_up_sampling[MAX_REF_NUM][P_C2][y][x] = INTERPOLATE_6tap_QUATER1( y_up_sampling[MAX_REF_NUM][P_A2][RFM_CLIP3( y-3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A2][RFM_CLIP3( y-2, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A2][y-1][x], y_up_sampling[MAX_REF_NUM][P_A2][y][x], y_up_sampling[MAX_REF_NUM][P_A2][y+1][x], y_up_sampling[MAX_REF_NUM][P_A2][y+2][x], y_up_sampling[MAX_REF_NUM][P_A2][RFM_CLIP3( y+3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A2][RFM_CLIP3( y+4, 1, img->p_height - 1 )][x] );
			y_up_sampling[MAX_REF_NUM][P_C4][y][x] = INTERPOLATE_6tap_QUATER3( y_up_sampling[MAX_REF_NUM][P_A2][RFM_CLIP3( y-3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A2][RFM_CLIP3( y-2, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A2][y-1][x], y_up_sampling[MAX_REF_NUM][P_A2][y][x], y_up_sampling[MAX_REF_NUM][P_A2][y+1][x], y_up_sampling[MAX_REF_NUM][P_A2][y+2][x], y_up_sampling[MAX_REF_NUM][P_A2][RFM_CLIP3( y+3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A2][RFM_CLIP3( y+4, 1, img->p_height - 1 )][x] );
			y_up_sampling[MAX_REF_NUM][P_D2][y][x] = INTERPOLATE_6tap_HALF(    y_up_sampling[MAX_REF_NUM][P_A2][RFM_CLIP3( y-3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A2][RFM_CLIP3( y-2, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A2][y-1][x], y_up_sampling[MAX_REF_NUM][P_A2][y][x], y_up_sampling[MAX_REF_NUM][P_A2][y+1][x], y_up_sampling[MAX_REF_NUM][P_A2][y+2][x], y_up_sampling[MAX_REF_NUM][P_A2][RFM_CLIP3( y+3, 1, img->p_height - 1 )][x], y_up_sampling[MAX_REF_NUM][P_A2][RFM_CLIP3( y+4, 1, img->p_height - 1 )][x] );

#endif

		}
	}

	y=0;
	for( x=0; x<img->p_width; x++ )
	{
		//x1=RFM_CLIP3(x,minx-1,maxx+1);

		//C
		y_up_sampling[MAX_REF_NUM][P_C][0][x] = INTERPOLATE_6tap_HALF( y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][1][x], y_up_sampling[MAX_REF_NUM][P_A][2][x], y_up_sampling[MAX_REF_NUM][P_A][3][x], y_up_sampling[MAX_REF_NUM][P_A][4][x] );
		y_up_sampling[MAX_REF_NUM][P_C][img->p_height-1][x] = INTERPOLATE_6tap_HALF( y_up_sampling[MAX_REF_NUM][P_A][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x] );
		y_up_sampling[MAX_REF_NUM][P_C][img->p_height-2][x] = INTERPOLATE_6tap_HALF( y_up_sampling[MAX_REF_NUM][P_A][img->p_height-5][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x] );
#if QUARTER_PIXEL
		//C1
		y_up_sampling[MAX_REF_NUM][P_C1][0][x] = INTERPOLATE_6tap_QUATER1( y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][1][x], y_up_sampling[MAX_REF_NUM][P_A1][2][x], y_up_sampling[MAX_REF_NUM][P_A1][3][x], y_up_sampling[MAX_REF_NUM][P_A1][4][x] );
		y_up_sampling[MAX_REF_NUM][P_C1][img->p_height-1][x] = INTERPOLATE_6tap_QUATER1( y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x] );
		y_up_sampling[MAX_REF_NUM][P_C1][img->p_height-2][x] = INTERPOLATE_6tap_QUATER1( y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-5][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x] );

		//D1
		y_up_sampling[MAX_REF_NUM][P_D1][0][x] = INTERPOLATE_6tap_HALF( y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][1][x], y_up_sampling[MAX_REF_NUM][P_A1][2][x], y_up_sampling[MAX_REF_NUM][P_A1][3][x], y_up_sampling[MAX_REF_NUM][P_A1][4][x] );
		y_up_sampling[MAX_REF_NUM][P_D1][img->p_height-1][x] = INTERPOLATE_6tap_HALF( y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x] );
		y_up_sampling[MAX_REF_NUM][P_D1][img->p_height-2][x] = INTERPOLATE_6tap_HALF( y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-5][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x] );

		//C3
		y_up_sampling[MAX_REF_NUM][P_C3][0][x] = INTERPOLATE_6tap_QUATER3( y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][1][x], y_up_sampling[MAX_REF_NUM][P_A1][2][x], y_up_sampling[MAX_REF_NUM][P_A1][3][x], y_up_sampling[MAX_REF_NUM][P_A1][4][x] );
		y_up_sampling[MAX_REF_NUM][P_C3][img->p_height-1][x] = INTERPOLATE_6tap_QUATER3( y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x] );
		y_up_sampling[MAX_REF_NUM][P_C3][img->p_height-2][x] = INTERPOLATE_6tap_QUATER3( y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-5][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x] );

		//D3,C,D4
		//D3
		y_up_sampling[MAX_REF_NUM][P_D3][0][x] = INTERPOLATE_6tap_QUATER1( y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][1][x], y_up_sampling[MAX_REF_NUM][P_A][2][x], y_up_sampling[MAX_REF_NUM][P_A][3][x], y_up_sampling[MAX_REF_NUM][P_A][4][x] );
		y_up_sampling[MAX_REF_NUM][P_D3][img->p_height-1][x] = INTERPOLATE_6tap_QUATER1( y_up_sampling[MAX_REF_NUM][P_A][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x] );
		y_up_sampling[MAX_REF_NUM][P_D3][img->p_height-2][x] = INTERPOLATE_6tap_QUATER1( y_up_sampling[MAX_REF_NUM][P_A][img->p_height-5][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x] );


		//D4
		y_up_sampling[MAX_REF_NUM][P_D4][0][x] = INTERPOLATE_6tap_QUATER3( y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][1][x], y_up_sampling[MAX_REF_NUM][P_A][2][x], y_up_sampling[MAX_REF_NUM][P_A][3][x], y_up_sampling[MAX_REF_NUM][P_A][4][x] );
		y_up_sampling[MAX_REF_NUM][P_D4][img->p_height-1][x] = INTERPOLATE_6tap_QUATER3( y_up_sampling[MAX_REF_NUM][P_A][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x] );
		y_up_sampling[MAX_REF_NUM][P_D4][img->p_height-2][x] = INTERPOLATE_6tap_QUATER3( y_up_sampling[MAX_REF_NUM][P_A][img->p_height-5][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x] );

		//C2,D2,C4
		//C2
		y_up_sampling[MAX_REF_NUM][P_C2][0][x] = INTERPOLATE_6tap_QUATER1( y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][1][x], y_up_sampling[MAX_REF_NUM][P_A2][2][x], y_up_sampling[MAX_REF_NUM][P_A2][3][x], y_up_sampling[MAX_REF_NUM][P_A2][4][x] );
		y_up_sampling[MAX_REF_NUM][P_C2][img->p_height-1][x] = INTERPOLATE_6tap_QUATER1( y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x] );
		y_up_sampling[MAX_REF_NUM][P_C2][img->p_height-2][x] = INTERPOLATE_6tap_QUATER1( y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-5][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x] );

		//D2
		y_up_sampling[MAX_REF_NUM][P_D2][0][x] = INTERPOLATE_6tap_HALF( y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][1][x], y_up_sampling[MAX_REF_NUM][P_A2][2][x], y_up_sampling[MAX_REF_NUM][P_A2][3][x], y_up_sampling[MAX_REF_NUM][P_A2][4][x] );
		y_up_sampling[MAX_REF_NUM][P_D2][img->p_height-1][x] = INTERPOLATE_6tap_HALF( y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x] );
		y_up_sampling[MAX_REF_NUM][P_D2][img->p_height-2][x] = INTERPOLATE_6tap_HALF( y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-5][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x] );

		//C4
		y_up_sampling[MAX_REF_NUM][P_C4][0][x] = INTERPOLATE_6tap_QUATER3( y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][1][x], y_up_sampling[MAX_REF_NUM][P_A2][2][x], y_up_sampling[MAX_REF_NUM][P_A2][3][x], y_up_sampling[MAX_REF_NUM][P_A2][4][x] );
		y_up_sampling[MAX_REF_NUM][P_C4][img->p_height-1][x] = INTERPOLATE_6tap_QUATER3( y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x] );
		y_up_sampling[MAX_REF_NUM][P_C4][img->p_height-2][x] = INTERPOLATE_6tap_QUATER3( y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-5][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-4][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x] );
#endif		

	}


}
#else
static void up_sampling()
{
    int x, y, pos;
    byte ***rec = img_y_rec_space;
#if QUARTER_2D
    static const int FHalf [6] = {4, -25, 149, 149, -25, 4};
    static const int FQuad [6] = {-1, -5, 220,  60, -22, 4};
#endif
    padding_img( img_y_rec_space[MAX_REF_NUM] );
#if QUARTER_2D
    //interpolation
    for ( y = 1; y < img->p_height-2; y++ )
    {
        for ( x = 1; x < img->p_width-2; x++ )
        {
            //x1=RFM_CLIP3(x,minx,maxx);
            //y1=RFM_CLIP3(y,miny,maxy);
            y_up_sampling[MAX_REF_NUM][P_A][y][x] = INTERPOLATE2D( FHalf,
                                                    rec[MAX_REF_NUM][y][RFM_CLIP3( x-2, 1, img->p_width - 3 )],
                                                    rec[MAX_REF_NUM][y][x-1],
                                                    rec[MAX_REF_NUM][y][x  ],
                                                    rec[MAX_REF_NUM][y][x+1],
                                                    rec[MAX_REF_NUM][y][x+2],
                                                    rec[MAX_REF_NUM][y][RFM_CLIP3( x+3, 1, img->p_width-3 )] );

            y_up_sampling[MAX_REF_NUM][P_B][y][x] = INTERPOLATE2D( FHalf,
                                                    rec[MAX_REF_NUM][RFM_CLIP3( y-2, 1, img->p_height - 3 )][x],
                                                    rec[MAX_REF_NUM][y-1][x],
                                                    rec[MAX_REF_NUM][y][x],
                                                    rec[MAX_REF_NUM][y+1][x],
                                                    rec[MAX_REF_NUM][y+2][x],
                                                    rec[MAX_REF_NUM][RFM_CLIP3( y+3, 1, img->p_height - 3 )][x] );

#if QUARTER_PIXEL
            y_up_sampling[MAX_REF_NUM][P_A1][y][x] = INTERPOLATE2D( FQuad,
                    rec[MAX_REF_NUM][y][RFM_CLIP3( x-2, 1, img->p_width - 3 )],
                    rec[MAX_REF_NUM][y][x-1],
                    rec[MAX_REF_NUM][y][x  ],
                    rec[MAX_REF_NUM][y][x+1],
                    rec[MAX_REF_NUM][y][x+2],
                    rec[MAX_REF_NUM][y][RFM_CLIP3( x+3, 1, img->p_width-3 )] ); //dw 20110808

            y_up_sampling[MAX_REF_NUM][P_A2][y][x] = INTERPOLATE2D_REV( FQuad,
                    rec[MAX_REF_NUM][y][RFM_CLIP3( x-2, 1, img->p_width - 3 )],
                    rec[MAX_REF_NUM][y][x-1],
                    rec[MAX_REF_NUM][y][x  ],
                    rec[MAX_REF_NUM][y][x+1],
                    rec[MAX_REF_NUM][y][x+2],
                    rec[MAX_REF_NUM][y][RFM_CLIP3( x+3, 1, img->p_width-3 )] ); //dw 20110808


            y_up_sampling[MAX_REF_NUM][P_B1][y][x] = INTERPOLATE2D( FQuad,
                    rec[MAX_REF_NUM][RFM_CLIP3( y-2, 1, img->p_height - 3 )][x],
                    rec[MAX_REF_NUM][y-1][x],
                    rec[MAX_REF_NUM][y][x],
                    rec[MAX_REF_NUM][y+1][x],
                    rec[MAX_REF_NUM][y+2][x],
                    rec[MAX_REF_NUM][RFM_CLIP3( y+3, 1, img->p_height - 3 )][x] ); //dw 20110808

            y_up_sampling[MAX_REF_NUM][P_B2][y][x] = INTERPOLATE2D_REV( FQuad,
                    rec[MAX_REF_NUM][RFM_CLIP3( y-2, 1, img->p_height - 3 )][x],
                    rec[MAX_REF_NUM][y-1][x],
                    rec[MAX_REF_NUM][y][x],
                    rec[MAX_REF_NUM][y+1][x],
                    rec[MAX_REF_NUM][y+2][x],
                    rec[MAX_REF_NUM][RFM_CLIP3( y+3, 1, img->p_height - 3 )][x] ); //dw 20110808
        }
    }
#endif
#else
    //interpolation
    for ( y = 1; y < img->p_height-2; y++ )
    {
        for ( x = 1; x < img->p_width-2; x++ )
        {
            //x1=RFM_CLIP3(x,minx,maxx);
            //y1=RFM_CLIP3(y,miny,maxy);
            y_up_sampling[MAX_REF_NUM][P_A][y][x] = INTERPOLATE( rec[MAX_REF_NUM][y][x-1], rec[MAX_REF_NUM][y][x], rec[MAX_REF_NUM][y][x+1], rec[MAX_REF_NUM][y][x+2] );
            y_up_sampling[MAX_REF_NUM][P_B][y][x] = INTERPOLATE( rec[MAX_REF_NUM][y-1][x], rec[MAX_REF_NUM][y][x], rec[MAX_REF_NUM][y+1][x], rec[MAX_REF_NUM][y+2][x] );
#if QUARTER_PIXEL
            y_up_sampling[MAX_REF_NUM][P_A1][y][x] = INTERPOLATE_QUATER1( rec[MAX_REF_NUM][y][x-1], rec[MAX_REF_NUM][y][x], rec[MAX_REF_NUM][y][x+1], rec[MAX_REF_NUM][y][x+2] ); //dw 20110808
            y_up_sampling[MAX_REF_NUM][P_A2][y][x] = INTERPOLATE_QUATER2( rec[MAX_REF_NUM][y][x-1], rec[MAX_REF_NUM][y][x], rec[MAX_REF_NUM][y][x+1], rec[MAX_REF_NUM][y][x+2] ); //dw 20110808
            y_up_sampling[MAX_REF_NUM][P_B1][y][x] = INTERPOLATE_QUATER1( rec[MAX_REF_NUM][y-1][x], rec[MAX_REF_NUM][y][x], rec[MAX_REF_NUM][y+1][x], rec[MAX_REF_NUM][y+2][x] ); //dw 20110808
            y_up_sampling[MAX_REF_NUM][P_B2][y][x] = INTERPOLATE_QUATER2( rec[MAX_REF_NUM][y-1][x], rec[MAX_REF_NUM][y][x], rec[MAX_REF_NUM][y+1][x], rec[MAX_REF_NUM][y+2][x] ); //dw 20110808
#endif
        }
    }
#endif
    y=0;
    for( x=1; x<img->p_width-2; x++ )
    {
        y_up_sampling[MAX_REF_NUM][P_A][0][x]=y_up_sampling[MAX_REF_NUM][P_A][1][x];
        y_up_sampling[MAX_REF_NUM][P_B][0][x]=y_up_sampling[MAX_REF_NUM][P_B][1][x];
        y_up_sampling[MAX_REF_NUM][P_A][img->p_height-2][x]=y_up_sampling[MAX_REF_NUM][P_A][img->p_height-3][x];
        y_up_sampling[MAX_REF_NUM][P_B][img->p_height-2][x]=y_up_sampling[MAX_REF_NUM][P_B][img->p_height-3][x];
        y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x]=y_up_sampling[MAX_REF_NUM][P_A][img->p_height-3][x];
        y_up_sampling[MAX_REF_NUM][P_B][img->p_height-1][x]=y_up_sampling[MAX_REF_NUM][P_B][img->p_height-3][x];
#if QUARTER_PIXEL
        y_up_sampling[MAX_REF_NUM][P_A1][0][x]=y_up_sampling[MAX_REF_NUM][P_A1][1][x];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_B1][0][x]=y_up_sampling[MAX_REF_NUM][P_B1][1][x];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-2][x]=y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-3][x];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_B1][img->p_height-2][x]=y_up_sampling[MAX_REF_NUM][P_B1][img->p_height-3][x];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x]=y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-3][x];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_B1][img->p_height-1][x]=y_up_sampling[MAX_REF_NUM][P_B1][img->p_height-3][x];  //dw 20110808

        y_up_sampling[MAX_REF_NUM][P_A2][0][x]=y_up_sampling[MAX_REF_NUM][P_A2][1][x];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_B2][0][x]=y_up_sampling[MAX_REF_NUM][P_B2][1][x];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-2][x]=y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-3][x];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_B2][img->p_height-2][x]=y_up_sampling[MAX_REF_NUM][P_B2][img->p_height-3][x];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x]=y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-3][x];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_B2][img->p_height-1][x]=y_up_sampling[MAX_REF_NUM][P_B2][img->p_height-3][x];  //dw 20110808
#endif
    }
    for( y=0; y<img->p_height; y++ )
    {
        y_up_sampling[MAX_REF_NUM][P_A][y][0]=y_up_sampling[MAX_REF_NUM][P_A][y][1];
        y_up_sampling[MAX_REF_NUM][P_B][y][0]=y_up_sampling[MAX_REF_NUM][P_B][y][1];
        y_up_sampling[MAX_REF_NUM][P_A][y][img->p_width-2]=y_up_sampling[MAX_REF_NUM][P_A][y][img->p_width-3];
        y_up_sampling[MAX_REF_NUM][P_B][y][img->p_width-2]=y_up_sampling[MAX_REF_NUM][P_B][y][img->p_width-3];
        y_up_sampling[MAX_REF_NUM][P_A][y][img->p_width-1]=y_up_sampling[MAX_REF_NUM][P_A][y][img->p_width-3];
        y_up_sampling[MAX_REF_NUM][P_B][y][img->p_width-1]=y_up_sampling[MAX_REF_NUM][P_B][y][img->p_width-3];
#if QUARTER_PIXEL
        y_up_sampling[MAX_REF_NUM][P_A1][y][0]=y_up_sampling[MAX_REF_NUM][P_A1][y][1];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_B1][y][0]=y_up_sampling[MAX_REF_NUM][P_B1][y][1];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_A1][y][img->p_width-2]=y_up_sampling[MAX_REF_NUM][P_A1][y][img->p_width-3];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_B1][y][img->p_width-2]=y_up_sampling[MAX_REF_NUM][P_B1][y][img->p_width-3];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_A1][y][img->p_width-1]=y_up_sampling[MAX_REF_NUM][P_A1][y][img->p_width-3];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_B1][y][img->p_width-1]=y_up_sampling[MAX_REF_NUM][P_B1][y][img->p_width-3];  //dw 20110808

        y_up_sampling[MAX_REF_NUM][P_A2][y][0]=y_up_sampling[MAX_REF_NUM][P_A2][y][1];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_B2][y][0]=y_up_sampling[MAX_REF_NUM][P_B2][y][1];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_A2][y][img->p_width-2]=y_up_sampling[MAX_REF_NUM][P_A2][y][img->p_width-3];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_B2][y][img->p_width-2]=y_up_sampling[MAX_REF_NUM][P_B2][y][img->p_width-3];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_A2][y][img->p_width-1]=y_up_sampling[MAX_REF_NUM][P_A2][y][img->p_width-3];  //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_B2][y][img->p_width-1]=y_up_sampling[MAX_REF_NUM][P_B2][y][img->p_width-3];  //dw 20110808
#endif
    }



    //padding:
    for ( pos = P_A; pos <= P_B; pos++ )
    {
        //padding_img(y_up_sampling[MAX_REF_NUM][pos]);
    }

#if QUARTER_2D
    for ( y = 1; y < img->p_height-2; y++ )
    {
        for ( x = 0; x < img->p_width; x++ )
        {
            //x1=RFM_CLIP3(x,minx-1,maxx+1);
            //y1=RFM_CLIP3(y,miny-1,maxy+1);
            //y_up_sampling[MAX_REF_NUM][P_C][y][x] = INTERPOLATE(y_up_sampling[MAX_REF_NUM][P_A][y][x-1], y_up_sampling[MAX_REF_NUM][P_A][y][x], y_up_sampling[MAX_REF_NUM][P_A][y][x+1], y_up_sampling[MAX_REF_NUM][P_A][y][x+2]);
            //y_up_sampling[MAX_REF_NUM][P_C][y][x] = INTERPOLATE(y_up_sampling[MAX_REF_NUM][P_A][y1-1][x1], y_up_sampling[MAX_REF_NUM][P_A][y1][x1], y_up_sampling[MAX_REF_NUM][P_A][y1+1][x1], y_up_sampling[MAX_REF_NUM][P_A][y1+2][x1]);
            y_up_sampling[MAX_REF_NUM][P_C][y][x] = INTERPOLATE2D_NA( FHalf,
                                                    y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y-2, 1, img->p_height - 3 )][x],
                                                    y_up_sampling[MAX_REF_NUM][P_A][y-1][x],
                                                    y_up_sampling[MAX_REF_NUM][P_A][y][x],
                                                    y_up_sampling[MAX_REF_NUM][P_A][y+1][x],
                                                    y_up_sampling[MAX_REF_NUM][P_A][y+2][x],
                                                    y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y+3, 1, img->p_height - 3 )][x] );
#if QUARTER_PIXEL

            // rec[MAX_REF_NUM]
            y_up_sampling[MAX_REF_NUM][P_C1][y][x] = INTERPOLATE2D( FQuad,
                    rec[MAX_REF_NUM][RFM_CLIP3( y-2, 1, img->p_height - 3 )][RFM_CLIP3( x-2, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y-1, 1, img->p_height - 3 )][RFM_CLIP3( x-1, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y  , 1, img->p_height - 3 )][RFM_CLIP3( x  , 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y+1, 1, img->p_height - 3 )][RFM_CLIP3( x+1, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y+2, 1, img->p_height - 3 )][RFM_CLIP3( x+2, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y+3, 1, img->p_height - 3 )][RFM_CLIP3( x+3, 0, img->p_width - 1 )] ); //dw 20110808

            y_up_sampling[MAX_REF_NUM][P_C2][y][x] = INTERPOLATE2D( FQuad,
                    rec[MAX_REF_NUM][RFM_CLIP3( y-2, 1, img->p_height - 3 )][RFM_CLIP3( x+3, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y-1, 1, img->p_height - 3 )][RFM_CLIP3( x+2, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y  , 1, img->p_height - 3 )][RFM_CLIP3( x+1, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y+1, 1, img->p_height - 3 )][RFM_CLIP3( x  , 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y+2, 1, img->p_height - 3 )][RFM_CLIP3( x-1, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y+3, 1, img->p_height - 3 )][RFM_CLIP3( x-2, 0, img->p_width - 1 )] ); //dw 20110808

            y_up_sampling[MAX_REF_NUM][P_C3][y][x] = INTERPOLATE2D_REV( FQuad,
                    rec[MAX_REF_NUM][RFM_CLIP3( y-2, 1, img->p_height - 3 )][RFM_CLIP3( x+3, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y-1, 1, img->p_height - 3 )][RFM_CLIP3( x+2, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y  , 1, img->p_height - 3 )][RFM_CLIP3( x+1, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y+1, 1, img->p_height - 3 )][RFM_CLIP3( x  , 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y+2, 1, img->p_height - 3 )][RFM_CLIP3( x-1, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y+3, 1, img->p_height - 3 )][RFM_CLIP3( x-2, 0, img->p_width - 1 )] ); //dw 20110808

            y_up_sampling[MAX_REF_NUM][P_C4][y][x] = INTERPOLATE2D_REV( FQuad,
                    rec[MAX_REF_NUM][RFM_CLIP3( y-2, 1, img->p_height - 3 )][RFM_CLIP3( x-2, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y-1, 1, img->p_height - 3 )][RFM_CLIP3( x-1, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y  , 1, img->p_height - 3 )][RFM_CLIP3( x  , 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y+1, 1, img->p_height - 3 )][RFM_CLIP3( x+1, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y+2, 1, img->p_height - 3 )][RFM_CLIP3( x+2, 0, img->p_width - 1 )],
                    rec[MAX_REF_NUM][RFM_CLIP3( y+3, 1, img->p_height - 3 )][RFM_CLIP3( x+3, 0, img->p_width - 1 )] ); //dw 20110808

            y_up_sampling[MAX_REF_NUM][P_D1][y][x] = INTERPOLATE2D_NA( FQuad,
                    y_up_sampling[MAX_REF_NUM][P_B][y][RFM_CLIP3( x-2, 0, img->p_width - 1 )],
                    y_up_sampling[MAX_REF_NUM][P_B][y][RFM_CLIP3( x-1, 0, img->p_width - 1 )],
                    y_up_sampling[MAX_REF_NUM][P_B][y][x],
                    y_up_sampling[MAX_REF_NUM][P_B][y][RFM_CLIP3( x+1, 0, img->p_width - 1 )],
                    y_up_sampling[MAX_REF_NUM][P_B][y][RFM_CLIP3( x+2, 0, img->p_width - 1 )],
                    y_up_sampling[MAX_REF_NUM][P_B][y][RFM_CLIP3( x+3, 0, img->p_width - 1 )] ); //dw 20110808

            y_up_sampling[MAX_REF_NUM][P_D2][y][x] = INTERPOLATE2D_NA_REV( FQuad,
                    y_up_sampling[MAX_REF_NUM][P_B][y][RFM_CLIP3( x-2, 0, img->p_width - 1 )],
                    y_up_sampling[MAX_REF_NUM][P_B][y][RFM_CLIP3( x-1, 0, img->p_width - 1 )],
                    y_up_sampling[MAX_REF_NUM][P_B][y][x],
                    y_up_sampling[MAX_REF_NUM][P_B][y][RFM_CLIP3( x+1, 0, img->p_width - 1 )],
                    y_up_sampling[MAX_REF_NUM][P_B][y][RFM_CLIP3( x+2, 0, img->p_width - 1 )],
                    y_up_sampling[MAX_REF_NUM][P_B][y][RFM_CLIP3( x+3, 0, img->p_width - 1 )] ); //dw 20110808

            y_up_sampling[MAX_REF_NUM][P_D3][y][x] = INTERPOLATE2D_NA( FQuad,
                    y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y-2, 1, img->p_height - 3 )][x],
                    y_up_sampling[MAX_REF_NUM][P_A][y-1][x],
                    y_up_sampling[MAX_REF_NUM][P_A][y][x],
                    y_up_sampling[MAX_REF_NUM][P_A][y+1][x],
                    y_up_sampling[MAX_REF_NUM][P_A][y+2][x],
                    y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y+3, 1, img->p_height - 3 )][x] ); //dw 20110808

            y_up_sampling[MAX_REF_NUM][P_D4][y][x] = INTERPOLATE2D_NA_REV( FQuad,
                    y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y-2, 1, img->p_height - 3 )][x],
                    y_up_sampling[MAX_REF_NUM][P_A][y-1][x],
                    y_up_sampling[MAX_REF_NUM][P_A][y][x],
                    y_up_sampling[MAX_REF_NUM][P_A][y+1][x],
                    y_up_sampling[MAX_REF_NUM][P_A][y+2][x],
                    y_up_sampling[MAX_REF_NUM][P_A][RFM_CLIP3( y+3, 1, img->p_height - 3 )][x] ); //dw 20110808

#endif
        }
    }
#else
    for ( y = 1; y < img->p_height-2; y++ )
    {
        for ( x = 0; x < img->p_width; x++ )
        {
            //x1=RFM_CLIP3(x,minx-1,maxx+1);
            //y1=RFM_CLIP3(y,miny-1,maxy+1);
            //y_up_sampling[MAX_REF_NUM][P_C][y][x] = INTERPOLATE(y_up_sampling[MAX_REF_NUM][P_A][y][x-1], y_up_sampling[MAX_REF_NUM][P_A][y][x], y_up_sampling[MAX_REF_NUM][P_A][y][x+1], y_up_sampling[MAX_REF_NUM][P_A][y][x+2]);
            //y_up_sampling[MAX_REF_NUM][P_C][y][x] = INTERPOLATE(y_up_sampling[MAX_REF_NUM][P_A][y1-1][x1], y_up_sampling[MAX_REF_NUM][P_A][y1][x1], y_up_sampling[MAX_REF_NUM][P_A][y1+1][x1], y_up_sampling[MAX_REF_NUM][P_A][y1+2][x1]);
            y_up_sampling[MAX_REF_NUM][P_C][y][x] = INTERPOLATE( y_up_sampling[MAX_REF_NUM][P_A][y-1][x], y_up_sampling[MAX_REF_NUM][P_A][y][x], y_up_sampling[MAX_REF_NUM][P_A][y+1][x], y_up_sampling[MAX_REF_NUM][P_A][y+2][x] );
#if QUARTER_PIXEL
            y_up_sampling[MAX_REF_NUM][P_C1][y][x] = INTERPOLATE_QUATER1( y_up_sampling[MAX_REF_NUM][P_A1][y-1][x], y_up_sampling[MAX_REF_NUM][P_A1][y][x], y_up_sampling[MAX_REF_NUM][P_A1][y+1][x], y_up_sampling[MAX_REF_NUM][P_A1][y+2][x] ); //dw 20110808
            y_up_sampling[MAX_REF_NUM][P_C2][y][x] = INTERPOLATE_QUATER1( y_up_sampling[MAX_REF_NUM][P_A2][y-1][x], y_up_sampling[MAX_REF_NUM][P_A2][y][x], y_up_sampling[MAX_REF_NUM][P_A2][y+1][x], y_up_sampling[MAX_REF_NUM][P_A2][y+2][x] ); //dw 20110808
            y_up_sampling[MAX_REF_NUM][P_C3][y][x] = INTERPOLATE_QUATER2( y_up_sampling[MAX_REF_NUM][P_A1][y-1][x], y_up_sampling[MAX_REF_NUM][P_A1][y][x], y_up_sampling[MAX_REF_NUM][P_A1][y+1][x], y_up_sampling[MAX_REF_NUM][P_A1][y+2][x] ); //dw 20110808
            y_up_sampling[MAX_REF_NUM][P_C4][y][x] = INTERPOLATE_QUATER2( y_up_sampling[MAX_REF_NUM][P_A2][y-1][x], y_up_sampling[MAX_REF_NUM][P_A2][y][x], y_up_sampling[MAX_REF_NUM][P_A2][y+1][x], y_up_sampling[MAX_REF_NUM][P_A2][y+2][x] ); //dw 20110808

            y_up_sampling[MAX_REF_NUM][P_D1][y][x] = INTERPOLATE( y_up_sampling[MAX_REF_NUM][P_A1][y-1][x], y_up_sampling[MAX_REF_NUM][P_A1][y][x], y_up_sampling[MAX_REF_NUM][P_A1][y+1][x], y_up_sampling[MAX_REF_NUM][P_A1][y+2][x] ); //dw 20110808
            y_up_sampling[MAX_REF_NUM][P_D2][y][x] = INTERPOLATE( y_up_sampling[MAX_REF_NUM][P_A2][y-1][x], y_up_sampling[MAX_REF_NUM][P_A2][y][x], y_up_sampling[MAX_REF_NUM][P_A2][y+1][x], y_up_sampling[MAX_REF_NUM][P_A2][y+2][x] ); //dw 20110808
            y_up_sampling[MAX_REF_NUM][P_D3][y][x] = INTERPOLATE_QUATER1( y_up_sampling[MAX_REF_NUM][P_A][y-1][x], y_up_sampling[MAX_REF_NUM][P_A][y][x], y_up_sampling[MAX_REF_NUM][P_A][y+1][x], y_up_sampling[MAX_REF_NUM][P_A][y+2][x] ); //dw 20110808
            y_up_sampling[MAX_REF_NUM][P_D4][y][x] = INTERPOLATE_QUATER2( y_up_sampling[MAX_REF_NUM][P_A][y-1][x], y_up_sampling[MAX_REF_NUM][P_A][y][x], y_up_sampling[MAX_REF_NUM][P_A][y+1][x], y_up_sampling[MAX_REF_NUM][P_A][y+2][x] ); //dw 20110808
#endif
        }
    }
#endif
    y=0;
    for( x=0; x<img->p_width; x++ )
    {
        //x1=RFM_CLIP3(x,minx-1,maxx+1);
        y_up_sampling[MAX_REF_NUM][P_C][0][x] = INTERPOLATE( y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][1][x], y_up_sampling[MAX_REF_NUM][P_A][2][x] );
        y_up_sampling[MAX_REF_NUM][P_C][img->p_height-1][x] = INTERPOLATE( y_up_sampling[MAX_REF_NUM][P_A][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x] );
        y_up_sampling[MAX_REF_NUM][P_C][img->p_height-2][x] = INTERPOLATE( y_up_sampling[MAX_REF_NUM][P_A][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x] );
#if QUARTER_PIXEL
        y_up_sampling[MAX_REF_NUM][P_C1][0][x] = INTERPOLATE_QUATER1( y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][1][x], y_up_sampling[MAX_REF_NUM][P_A1][2][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_C1][img->p_height-1][x] = INTERPOLATE_QUATER1( y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_C1][img->p_height-2][x] = INTERPOLATE_QUATER1( y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x] ); //dw 20110808

        y_up_sampling[MAX_REF_NUM][P_C2][0][x] = INTERPOLATE_QUATER1( y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][1][x], y_up_sampling[MAX_REF_NUM][P_A2][2][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_C2][img->p_height-1][x] = INTERPOLATE_QUATER1( y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_C2][img->p_height-2][x] = INTERPOLATE_QUATER1( y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x] ); //dw 20110808

        y_up_sampling[MAX_REF_NUM][P_C3][0][x] = INTERPOLATE_QUATER2( y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][1][x], y_up_sampling[MAX_REF_NUM][P_A1][2][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_C3][img->p_height-1][x] = INTERPOLATE_QUATER2( y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_C3][img->p_height-2][x] = INTERPOLATE_QUATER2( y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x] ); //dw 20110808

        y_up_sampling[MAX_REF_NUM][P_C4][0][x] = INTERPOLATE_QUATER2( y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][1][x], y_up_sampling[MAX_REF_NUM][P_A2][2][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_C4][img->p_height-1][x] = INTERPOLATE_QUATER2( y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_C4][img->p_height-2][x] = INTERPOLATE_QUATER2( y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x] ); //dw 20110808

        y_up_sampling[MAX_REF_NUM][P_D1][0][x] = INTERPOLATE( y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][0][x], y_up_sampling[MAX_REF_NUM][P_A1][1][x], y_up_sampling[MAX_REF_NUM][P_A1][2][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_D1][img->p_height-1][x] = INTERPOLATE( y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_D1][img->p_height-2][x] = INTERPOLATE( y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A1][img->p_height-1][x] ); //dw 20110808

        y_up_sampling[MAX_REF_NUM][P_D2][0][x] = INTERPOLATE( y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][0][x], y_up_sampling[MAX_REF_NUM][P_A2][1][x], y_up_sampling[MAX_REF_NUM][P_A2][2][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_D2][img->p_height-1][x] = INTERPOLATE( y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_D2][img->p_height-2][x] = INTERPOLATE( y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A2][img->p_height-1][x] ); //dw 20110808

        y_up_sampling[MAX_REF_NUM][P_D3][0][x] = INTERPOLATE_QUATER1( y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][1][x], y_up_sampling[MAX_REF_NUM][P_A][2][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_D3][img->p_height-1][x] = INTERPOLATE_QUATER1( y_up_sampling[MAX_REF_NUM][P_A][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_D3][img->p_height-2][x] = INTERPOLATE_QUATER1( y_up_sampling[MAX_REF_NUM][P_A][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x] ); //dw 20110808

        y_up_sampling[MAX_REF_NUM][P_D4][0][x] = INTERPOLATE_QUATER2( y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][0][x], y_up_sampling[MAX_REF_NUM][P_A][1][x], y_up_sampling[MAX_REF_NUM][P_A][2][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_D4][img->p_height-1][x] = INTERPOLATE_QUATER2( y_up_sampling[MAX_REF_NUM][P_A][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x] ); //dw 20110808
        y_up_sampling[MAX_REF_NUM][P_D4][img->p_height-2][x] = INTERPOLATE_QUATER2( y_up_sampling[MAX_REF_NUM][P_A][img->p_height-3][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-2][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x], y_up_sampling[MAX_REF_NUM][P_A][img->p_height-1][x] ); //dw 20110808
#endif
    }
    //padding_img(y_up_sampling[MAX_REF_NUM][P_C]);
}
#endif
/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
static void find_snr()
{
    int i, j;
    int diff_y, diff_u, diff_v;
    int impix;
    int uvformat = 4;

    impix = img->height * img->width;
    diff_y = diff_u = diff_v = 0;

    for ( i = 0; i < img->width; ++i )
    {
        for ( j = 0; j < img->height; ++j )
        {
            diff_y += img->quad[img_y_org[j][i] - p_img_y_rec[j][i]];
        }
    }

    for ( i = 0; i < img->width_cr; i++ )
    {
        for ( j = 0; j < img->height_cr; j++ )
        {
            diff_u += img->quad[img_uv_org[0][j][i] - p_img_uv_rec[0][j][i]];
            diff_v += img->quad[img_uv_org[1][j][i] - p_img_uv_rec[1][j][i]];
        }
    }

    if ( diff_y != 0 )
    {
        snr->snr_y = ( float )( 10 * log10( 65025 * ( float )impix / ( float )diff_y ) );
        snr->snr_u = ( float )( 10 * log10( 65025 * ( float )impix / ( float )( uvformat * diff_u ) ) );
        snr->snr_v = ( float )( 10 * log10( 65025 * ( float )impix / ( float )( uvformat * diff_v ) ) );
    }

    if ( img->number == 0 )
    {
        snr->snr_y1 = ( float )( 10 * log10( 65025 * ( float )impix / ( float )diff_y ) );
        snr->snr_u1 = ( float )( 10 * log10( 65025 * ( float )impix / ( float )( uvformat * diff_u ) ) );
        snr->snr_v1 = ( float )( 10 * log10( 65025 * ( float )impix / ( float )( uvformat * diff_v ) ) );
        snr->snr_ya = snr->snr_y1;
        snr->snr_ua = snr->snr_u1;
        snr->snr_va = snr->snr_v1;
    }
    else
    {
        snr->snr_ya = ( float )( snr->snr_ya * ( img->number + tot_b_frm_num ) + snr->snr_y ) / ( img->number + tot_b_frm_num + 1 );
        snr->snr_ua = ( float )( snr->snr_ua * ( img->number + tot_b_frm_num ) + snr->snr_u ) / ( img->number + tot_b_frm_num + 1 );
        snr->snr_va = ( float )( snr->snr_va * ( img->number + tot_b_frm_num ) + snr->snr_v ) / ( img->number + tot_b_frm_num + 1 );
    }
}

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
static void init_frame()
{
    int prevP_no, nextP_no;

    stat->bit_slice    = 0;
    img->curr_mb_nr    = 0;
    img->curr_slice_nr = 0;
    img->coded_mb_nr   = 0;

    img->mb_no_currSliceLastMB = ( input->slice_row_nr != 0 ) ? RFM_MIN( input->slice_row_nr * img->mb_width - 1, img->mb_width * img->mb_height - 1 ) : img->mb_width * img->mb_height - 1;
    img->mb_y     = img->mb_x  = 0;
    img->block8_y = img->pix_y = img->pix_c_y = 0;
    img->block8_x = img->pix_x = img->pix_c_x = 0;

    if ( img->type != B_IMG )
    {
        img->tr = img->number * img->p_interval;
        img->tr = RFM_MIN( img->tr, input->no_frames - 1 );
        picture_distance      = ( img->number * ( input->jumpd + 1 ) ) % 256;
        if ( !input->rc_enable )
        {
#ifdef OPT_HP
            if ( img->type == I_IMG )
                img->qp = input->qp_first + 3*(img->tag_hp==1) + 4*(img->tag_hp>=2);//hp coding
            else
                img->qp = input->qp_p + 3*(img->tag_hp==1) + 4*(img->tag_hp>=2);//hp coding
            img->vbs_qp_16x16 = img->qp + 3*(img->tag_hp==1) + 4*(img->tag_hp>=2);//hp coding  //  rong 0111  ABT +2??
            img->vbs_qp_4x4 = img->qp + 3*(img->tag_hp==1) + 4*(img->tag_hp>=2);//hp coding  //  rong 0111  ABT +2??
#else
            if ( img->type == I_IMG )
                img->qp = input->qp_first;
            else
                img->qp = input->qp_p;
            img->vbs_qp_16x16 = img->qp;  //  rong 0111  ABT +2??
            img->vbs_qp_4x4 = img->qp;  //  rong 0111  ABT +2??
#endif
        }
    }
    else
    {
        prevP_no         = ( img->number - 1 ) * img->p_interval;
        nextP_no         = ( img->number ) * img->p_interval;
        nextP_no         = RFM_MIN( nextP_no, input->no_frames - 1 );
        img->b_interval  = ( int )( ( float )( input->jumpd + 1 ) / ( input->succe_b + 1.0 ) + 0.49999 );
        img->tr          = prevP_no + img->b_interval * img->b_frame_to_code;
        picture_distance = ( ( img->number - 1 ) * ( input->jumpd + 1 ) + img->b_interval * img->b_frame_to_code ) % 256;

        if ( img->tr >= nextP_no )
            img->tr = nextP_no - 1;

        if ( !input->rc_enable )
            img->qp = input->qp_b;
        img->vbs_qp_4x4 = img->qp;  //  rong 0111  ABT +2??
    }
}

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
static void pic_header()
{
    int len = 0;

    img->cod_counter = 0;

    len += picture_header( 0 );
    img->number_of_header_bits += len;
    if ( img->basic_unit < img->total_mb_num )
        img->number_of_basic_unit_header_bits += len;
    stat->bit_slice                 += len;
    stat->bit_use_header[img->type] += len;
}


/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
static void report_first_frame( int tmp_time )
{
    //int bits;
    printf( "%3d(I)  %8d %4d %7.4f %7.4f %7.4f  %5d       %s \n", img->tr, stat->bit_ctr - stat->bit_ctr_n, img->qp, snr->snr_y, snr->snr_u, snr->snr_v, tmp_time, 1 /*img->picture_structure*/ ? "FRM" : "FLD" );

    if ( input->rc_enable )
    {
        //bits = stat->bit_ctr - stat->bit_ctr_n;
    }
    stat->bit_ctr_0 = stat->bit_ctr;
}

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
static void report_i( int tmp_time )
{
    printf( "%3d(I)  %8d %4d %7.4f %7.4f %7.4f  %5d %3s\n", img->tr, stat->bit_ctr - stat->bit_ctr_n, img->qp, snr->snr_y, snr->snr_u, snr->snr_v, tmp_time, 1 /*img->picture_structure*/ ? "FRM" : "FLD" );
}

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
static void report_b( int tmp_time )
{
    printf( "%3d(B)  %8d %4d %7.4f %7.4f %7.4f  %5d       %3s     %3d    \n", img->tr, stat->bit_ctr - stat->bit_ctr_n, img->qp, snr->snr_y, snr->snr_u, snr->snr_v, tmp_time, 1 /*img->picture_structure*/ ? "FRM" : "FLD", intra_num[img->type-1] );
}

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
static void report_p( int tmp_time )
{
    printf( "%3d(P)  %8d %4d %7.4f %7.4f %7.4f  %5d       %3s     %3d    \n", img->tr, stat->bit_ctr - stat->bit_ctr_n, img->qp, snr->snr_y, snr->snr_u, snr->snr_v, tmp_time, 1 /*img->picture_structure*/ ? "FRM" : "FLD", intra_num[img->type-1] );
}

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
static void switch_ref_buffer()
{
    const int ref_num = MAX_REF_NUM;
    int i, uv, pos;
    mv_t **tmp_mv;

    byte ** tmp_y_offset;
    byte ** tmp_uv_offset[2];
    byte ** tmp_img_y_rec_space;
    byte ** tmp_img_uv_rec_space[UV];
#if QUARTER_PIXEL
    byte ** tmp_y_upsampling_space[HALF_POS_NUM + QUATER_POS_NUM];
#else
    byte **tmp_y_upsampling_space[HALF_POS_NUM];
#endif

    tmp_img_y_rec_space = img_y_rec_space[ref_num];
    tmp_img_uv_rec_space[0] = img_uv_rec_space[0][ref_num];
    tmp_img_uv_rec_space[1] = img_uv_rec_space[1][ref_num];
    tmp_mv = frm_mv[ref_num];

#if QUARTER_PIXEL
    for ( pos = 0; pos < HALF_POS_NUM + QUATER_POS_NUM; pos++ )
#else
    for ( pos = 0; pos < HALF_POS_NUM; pos++ )
#endif
    {
        tmp_y_upsampling_space[pos] = y_up_sampling[ref_num][pos];
    }

    tmp_y_offset = img_y_rec_space_offset[ref_num];
    for ( uv=0; uv<UV; uv++ )
    {
        tmp_uv_offset[uv] = img_uv_rec_space_offset[uv][ref_num];
    }
    for ( i=ref_num; i>0; i-- )
    {
        img_y_rec_space[i] = img_y_rec_space[i-1];
        img_y_rec_space_offset[i] = img_y_rec_space_offset[i-1];

        for ( uv=0; uv<UV; uv++ )
        {
            img_uv_rec_space[uv][i] = img_uv_rec_space[uv][i-1];
            img_uv_rec_space_offset[uv][i] = img_uv_rec_space_offset[uv][i-1];
        }
#if QUARTER_PIXEL
        for ( pos = 0; pos < HALF_POS_NUM + QUATER_POS_NUM; pos++ )
#else
        for ( pos = 0; pos < HALF_POS_NUM; pos++ )
#endif
        {
            y_up_sampling[i][pos] = y_up_sampling[i-1][pos];
        }
        frm_mv[i]                 = frm_mv[i-1];
    }
    frm_mv[0] = tmp_mv;
    img_y_rec_space[0] = tmp_img_y_rec_space;
    img_y_rec_space_offset[0] = tmp_y_offset;
    for ( uv=0; uv<UV; uv++ )
    {
        img_uv_rec_space[uv][0] = tmp_img_uv_rec_space[uv];
        img_uv_rec_space_offset[uv][0] = tmp_uv_offset[uv];
    }
#if QUARTER_PIXEL
    for ( pos = 0; pos < HALF_POS_NUM + QUATER_POS_NUM; pos++ )
#else
    for ( pos = 0; pos < HALF_POS_NUM; pos++ )
#endif
    {
        y_up_sampling[0][pos] = tmp_y_upsampling_space[pos];
    }
}
#ifdef OPT_HP
static void swap_ref_buffer()
{
	const int ref_num = 1;
	int i, uv, pos;
	mv_t **tmp_mv;

	byte ** tmp_y_offset;
	byte ** tmp_uv_offset[2];
	byte ** tmp_img_y_rec_space;
	byte ** tmp_img_uv_rec_space[UV];
#if QUARTER_PIXEL
	byte ** tmp_y_upsampling_space[HALF_POS_NUM + QUATER_POS_NUM];
#else
	byte **tmp_y_upsampling_space[HALF_POS_NUM];
#endif

	tmp_img_y_rec_space = img_y_rec_space[ref_num];
	tmp_img_uv_rec_space[0] = img_uv_rec_space[0][ref_num];
	tmp_img_uv_rec_space[1] = img_uv_rec_space[1][ref_num];
	tmp_mv = frm_mv[ref_num];

#if QUARTER_PIXEL
	for ( pos = 0; pos < HALF_POS_NUM + QUATER_POS_NUM; pos++ )
#else
	for ( pos = 0; pos < HALF_POS_NUM; pos++ )
#endif
	{
		tmp_y_upsampling_space[pos] = y_up_sampling[ref_num][pos];
	}

	tmp_y_offset = img_y_rec_space_offset[ref_num];
	for ( uv=0; uv<UV; uv++ )
	{
		tmp_uv_offset[uv] = img_uv_rec_space_offset[uv][ref_num];
	}
	for ( i=ref_num; i>0; i-- )
	{
		img_y_rec_space[i] = img_y_rec_space[i-1];
		img_y_rec_space_offset[i] = img_y_rec_space_offset[i-1];

		for ( uv=0; uv<UV; uv++ )
		{
			img_uv_rec_space[uv][i] = img_uv_rec_space[uv][i-1];
			img_uv_rec_space_offset[uv][i] = img_uv_rec_space_offset[uv][i-1];
		}
#if QUARTER_PIXEL
		for ( pos = 0; pos < HALF_POS_NUM + QUATER_POS_NUM; pos++ )
#else
		for ( pos = 0; pos < HALF_POS_NUM; pos++ )
#endif
		{
			y_up_sampling[i][pos] = y_up_sampling[i-1][pos];
		}
		frm_mv[i]                 = frm_mv[i-1];
	}
	frm_mv[0] = tmp_mv;
	img_y_rec_space[0] = tmp_img_y_rec_space;
	img_y_rec_space_offset[0] = tmp_y_offset;
	for ( uv=0; uv<UV; uv++ )
	{
		img_uv_rec_space[uv][0] = tmp_img_uv_rec_space[uv];
		img_uv_rec_space_offset[uv][0] = tmp_uv_offset[uv];
	}
#if QUARTER_PIXEL
	for ( pos = 0; pos < HALF_POS_NUM + QUATER_POS_NUM; pos++ )
#else
	for ( pos = 0; pos < HALF_POS_NUM; pos++ )
#endif
	{
		y_up_sampling[0][pos] = tmp_y_upsampling_space[pos];
	}
}
#endif
/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/

static void write_recon_image()
{
    int i, j, k;
    int start = 0, inc = 1;

    int writeout_width     = ( img->width  - img->auto_crop_right );
    int writeout_height    = ( img->height - img->auto_crop_bottom );
    int writeout_width_cr  = ( img->width  - img->auto_crop_right ) / 2;
    int writeout_height_cr = ( img->height - img->auto_crop_bottom ) / ( input->chroma_format == 2 ? 1 : 2 );

#if DEBLOCK
    byte **output_y = p_img_y_rec_deblock;
    byte ***output_uv = p_img_uv_rec_deblock;
#else
    byte **output_y = p_img_y_rec;
    byte ***output_uv = p_img_uv_rec;
#endif

    unsigned char *buf;
    buf = malloc( writeout_width * writeout_height );

    if ( p_rec != -1 )
    {
        if ( img->type != B_IMG )
        {
            if ( ( input->succe_b == 0 ) || ( ( img->tr == input->no_frames - 1 ) && ( input->no_frames % ( input->succe_b + 1 ) == 2 ) ) )
            {
                for ( j = 0; j < writeout_height; j++ )
                    memcpy( buf + j * writeout_width, &( output_y[j][0] ), writeout_width );
                write( p_rec, buf, writeout_height * writeout_width );

                for ( j = 0; j < writeout_height_cr; j++ )
                    memcpy( buf + j * writeout_width_cr, &( output_uv[0][j][0] ), writeout_width_cr );
                write( p_rec, buf, writeout_height_cr * writeout_width_cr );

                for ( j = 0; j < writeout_height_cr; j++ )
                    memcpy( buf + j * writeout_width_cr, &( output_uv[1][j][0] ), writeout_width_cr );
                write( p_rec, buf, writeout_height_cr * writeout_width_cr );
            }
            else if ( img->number == 0 && input->succe_b != 0 )
            {
                for ( j = 0; j < writeout_height; j++ )
                    memcpy( buf + j * writeout_width, &( output_y[j][0] ), writeout_width );
                write( p_rec, buf, writeout_height * writeout_width );

                for ( j = 0; j < writeout_height_cr; j++ )
                    memcpy( buf + j * writeout_width_cr, &( output_uv[0][j][0] ), writeout_width_cr );
                write( p_rec, buf, writeout_height_cr * writeout_width_cr );

                for ( j = 0; j < writeout_height_cr; j++ )
                    memcpy( buf + j * writeout_width_cr, &( output_uv[1][j][0] ), writeout_width_cr );
                write( p_rec, buf, writeout_height_cr * writeout_width_cr );
            }
            else if ( img->number != 0 && input->succe_b != 0 )
            {
#if DEBLOCK  //added by ckj
                {
                    for( i = 0; i < img->height; i ++ )
                        memcpy( nextP_imgY_deblock[i], p_img_y_rec_deblock[i], img->width );
                    for( i = 0; i < img->height_cr; i ++ )
                    {
                        memcpy( nextP_imgUV_deblock[0][i], p_img_uv_rec_deblock[0][i], img->width_cr );
                        memcpy( nextP_imgUV_deblock[1][i], p_img_uv_rec_deblock[1][i], img->width_cr );
                    }
                }
#endif

                for ( i = start; i < writeout_height; i += inc )
                    for ( j = 0; j < writeout_width; j++ )
                        nextP_imgY[i][j] = output_y[i][j];

                for ( k = 0; k < 2; ++k )
                    for ( i = start; i < writeout_height_cr; i += inc )
                        for ( j = 0; j < writeout_width_cr; j++ )
                            nextP_imgUV[k][i][j] = output_uv[k][i][j];
            }
        }
        else
        {
            for ( j = 0; j < writeout_height; j++ )
                memcpy( buf + j * writeout_width, &( output_y[j][0] ), writeout_width );
            write( p_rec, buf, writeout_height * writeout_width );

            for ( j = 0; j < writeout_height_cr; j++ )
                memcpy( buf + j * writeout_width_cr, &( output_uv[0][j][0] ), writeout_width_cr );
            write( p_rec, buf, writeout_height_cr * writeout_width_cr );

            for ( j = 0; j < writeout_height_cr; j++ )
                memcpy( buf + j * writeout_width_cr, &( output_uv[1][j][0] ), writeout_width_cr );
            write( p_rec, buf, writeout_height_cr * writeout_width_cr );

            if ( ( img->b_frame_to_code == input->succe_b ) || ( img->tr == input->no_frames - 2 ) )
            {
#if DEBLOCK//added by ckj
                output_y = nextP_imgY_deblock;
                output_uv = nextP_imgUV_deblock;
#else
                output_y = nextP_imgY;
                output_uv = nextP_imgUV;
#endif

                for ( j = 0; j < writeout_height; j++ )
                    memcpy( buf + j * writeout_width, &( output_y[j][0] ), writeout_width );
                write( p_rec, buf, writeout_height * writeout_width );

                for ( j = 0; j < writeout_height_cr; j++ )
                    memcpy( buf + j * writeout_width_cr, &( output_uv[0][j][0] ), writeout_width_cr );
                write( p_rec, buf, writeout_height_cr * writeout_width_cr );

                for ( j = 0; j < writeout_height_cr; j++ )
                    memcpy( buf + j * writeout_width_cr, &( output_uv[1][j][0] ), writeout_width_cr );
                write( p_rec, buf, writeout_height_cr * writeout_width_cr );
            }
        }
    }

    free( buf );
}

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
static void read_one_frame( int frame_no_infile, int header_size, int xs, int ys )
{
    int x,y;
    int input_width_cr  = ( input->img_width/2 );
    int input_height_cr = ( input->img_height/2 );
    unsigned int  bytes_y = input->img_width * input->img_height; //add by wuzhongmou 0610
    const unsigned int  bytes_uv = input_width_cr * input_height_cr;  //422, 20080721

    int off_y  = input->img_width*input->img_height;
    int off_uv = input_width_cr * input_height_cr;  //422, 20080721
    const int framesize_in_bytes = bytes_y + 2*bytes_uv;
    byte *u_buffer,*v_buffer;


    //assert (FrameNumberInFile == FrameNoInFile);

    if ( lseek ( p_in, framesize_in_bytes * frame_no_infile + header_size, SEEK_SET )==-1 )
        error ( "ReadOneFrame: cannot fseek to (Header size) in p_in", -1 );

    if ( read ( p_in, imgY_org_buffer, bytes_y ) != ( int )bytes_y )
    {
        printf ( "ReadOneFrame: cannot read %d bytes from input file, unexpected EOF?, exiting", bytes_y );
        exit ( -1 );
    }

    if ( read ( p_in, imgY_org_buffer + off_y, bytes_uv ) != ( int )bytes_uv )
    {
        printf ( "ReadOneFrame: cannot read %d bytes from input file, unexpected EOF?, exiting", bytes_uv );
        exit ( -1 );
    }

    if ( read ( p_in, imgY_org_buffer + off_y + off_uv, bytes_uv ) != ( int )bytes_uv )
    {
        printf ( "ReadOneFrame: cannot read %d bytes from input file, unexpected EOF?, exiting", bytes_uv );
        exit ( -1 );
    }

    u_buffer=imgY_org_buffer+input->img_width*input->img_height;
    v_buffer=imgY_org_buffer+input->img_width*input->img_height*5/4;

    for ( y=0; y<input->img_height; y++ )
        for ( x=0; x<input->img_width; x++ )
            img_y_org [y][x] = imgY_org_buffer[y*input->img_width+x];

    for ( y=0; y<input_height_cr; y++ ) //X ZHENG, 422
        for ( x=0; x<input->img_width/2; x++ )
        {
            img_uv_org[0][y][x] = u_buffer[y*input->img_width/2+x];
            img_uv_org[1][y][x] = v_buffer[y*input->img_width/2+x];
        }

    // Y's width padding
    for ( y=0; y<input->img_height; y++ )
        for ( x=input->img_width; x<img->width; x++ )
            img_y_org [y][x] = img_y_org [y][x-1];

    //Y's padding bottom border
    for ( y=input->img_height; y<img->height; y++ )
        for ( x=0; x<img->width; x++ )
            img_y_org [y][x] = img_y_org [y-1][x];

    // UV's padding width
    for ( y=0; y<input_height_cr; y++ ) //X ZHENG, 422
        for ( x=input->img_width/2; x<img->width/2; x++ )
        {
            img_uv_org [0][y][x] = img_uv_org [0][y][x-1];
            img_uv_org [1][y][x] = img_uv_org [1][y][x-1];
        }

    // UV's padding bottom border
    for ( y=input_height_cr; y< img->height/2; y++ ) //X ZHENG, 422
        for ( x=0; x<img->width/2; x++ )
        {
            img_uv_org [0][y][x] = img_uv_org [0][y-1][x];
            img_uv_org [1][y][x] = img_uv_org [1][y-1][x];
        }
}

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
void encode_one_frame()
{
    int uv;
    time_t ltime1;
    time_t ltime2;

#if defined(WIN32) && !defined(__MINGW32__)
    struct _timeb tstruct1;
    struct _timeb tstruct2;
#else
    struct timeb tstruct1;
    struct timeb tstruct2;
#endif

    int tmp_time;
    int bits = 0;
    ftime( &tstruct1 );
    time( &ltime1 );
#ifdef OPT_HP
	if(input->hp_coding && img->type==P_IMG && img->number % HPGOPSIZE==0 && input->UseFME)//hierarchical p coding
	{
		int i,j,m,n;
		int total_static_blocks = 0;
		for ( i = 0; i < img->height/8; i++ ){
			for( j = 0; j < img->width/8; j++ )
			{
				for( m = 0; m < 2; m++ )
					for(n = 0; n <2; n++ )
						if( abs(last_frm_mv_4x4[i][j][m][n].x) + abs(last_frm_mv_4x4[i][j][m][n].y) < 16)
							total_static_blocks++;
			}
		}
		printf("total static blocks proportion = %5.3f\n",total_static_blocks/(1.0* img->height/4 * img->width/4));
		if( 4 * total_static_blocks > img->height/4 * img->width/4 )
			img->hp_coding = 1;
		else
			img->hp_coding = 0;
	}
	else if(img->type==I_IMG)
		img->hp_coding = 0;
	else if(input->hp_coding==0)
		img->hp_coding = 0;
	else if(input->UseFME)
		img->hp_coding = img->hp_coding;
	else
		img->hp_coding = 0;

	if(img->hp_coding)
	{
		if(img->number % HPGOPSIZE==0)
			img->tag_hp = 0;
		else if(img->number % HPGOPSIZE < HPGOPSIZE/2)
			img->tag_hp = 2;
		else if(img->number % HPGOPSIZE == HPGOPSIZE/2)
			img->tag_hp = 1;
		else if(img->number % HPGOPSIZE == HPGOPSIZE-1)
			img->tag_hp = 3;
	}
	else
		img->tag_hp = 0;
#endif
    init_frame();
    read_one_frame( img->tr, input->infile_header, input->img_width, input->img_height );

    curr_slice_byte_pos = 0;
    if ( input->rc_enable )
    {
        img->basic_unit = input->basic_unit;
        rc_init_pict( 1, 0, 1 );
        img->qp  = update_quant_para( 0 );
    }
    else
    {
        img->bbv_delay = 0xFFFF;
    }
    if ( img->type == B_IMG )
        tot_b_frm_num++;

    init_bs( curr_stream );
    pic_header();
    pic_data();

#if DEBLOCK
    CopyAndDeblockFrame( img, p_img_y_rec_deblock, p_img_uv_rec_deblock, p_img_y_rec, p_img_uv_rec );
#endif

    if ( img->type != B_IMG )
    {
#ifdef OPT_HP
		if( img->tag_hp <= 1 )
		{
#endif
        up_sampling();
        switch_ref_buffer();
#ifdef OPT_HP
		}
		else if( img->tag_hp == 2 )
		{
			;
		}
		else
		{
			swap_ref_buffer();
		}
#endif
    }
    write_bs_to_file();
    find_snr();

    time( &ltime2 );
    ftime( &tstruct2 );

    tmp_time = ( ltime2 * 1000 + tstruct2.millitm ) - ( ltime1 * 1000 + tstruct1.millitm );
    tot_time = tot_time + tmp_time;
    write_recon_image();

    p_img_y_rec = img_y_rec_space_offset[MAX_REF_NUM];
    for ( uv=0; uv<UV; uv++ )
    {
        p_img_uv_rec[uv] = img_uv_rec_space_offset[uv][MAX_REF_NUM];
    }

    if ( input->rc_enable )
    {
        bits = stat->bit_ctr - stat->bit_ctr_n;
        rc_update_pict_frame( bits );
    }

    if ( img->number == 0 )
        report_first_frame( tmp_time );
    else
    {
        if ( input->rc_enable )
        {
            bits       = stat->bit_ctr - Pprev_bits;
            Pprev_bits = stat->bit_ctr;
        }
        switch ( img->type )
        {
            case I_IMG:
                stat->bit_ctr_0 += stat->bit_ctr - stat->bit_ctr_n;
                report_i( tmp_time );
                break;
            case B_IMG:
                stat->bit_ctr_B += stat->bit_ctr - stat->bit_ctr_n;
                report_b( tmp_time );
                break;
            default:
                stat->bit_ctr_P += stat->bit_ctr - stat->bit_ctr_n;
                report_p( tmp_time );
        }
    }
    stat->bit_ctr_n = stat->bit_ctr;
    if ( input->rc_enable )
    {
        rc_update_pict( bits );
        if ( img->type == P_IMG && img->IFLAG == 0 )
            update_rc_model();
    }

#ifdef FastME
    if( input->UseFME == 1 )
    {
        int i,j;
        switch ( img->type )
        {
            case P_IMG:
                tr_lastP = img->tr;
                is_lastI = 0;
                for ( i = 0; i < img->height/8; i++ )
                    for( j = 0; j < img->width/8; j++ )
                        memcpy ( &( last_frm_mv_4x4[i][j][0][0] ), &( curr_frm_mv_4x4[i][j][0][0] ), sizeof( mv_t )* 2 * 2 );
                break;
            case B_IMG:
                tr_lastB = img->tr;
                for ( i = 0; i < img->height/8; i++ )
                    for( j = 0; j < img->width/8; j++ )
                        memcpy ( &( last_frm_bi_mv_4x4[i][j][0][0] ), &( curr_frm_bi_mv_4x4[i][j][0][0] ), sizeof( bi_mv_t )* 2 * 2 );

                break;
            case I_IMG:
                is_lastI = 1;
                break;
            default:
                break;
        }
    }
#endif
}


